import time
from selenium.webdriver.common.by import By

from common.Utils import QE_Driver

#测试问卷考试系统模板广场页面

class QE_TemplateTest:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/template"
        self.driver = QE_Driver.driver
        self.driver.get(self.url)

    #进行页面元素的查找检查
    def TemplateCheckElement(self):
        #检查页面标题
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-heading > div > span")
        #检查模板名字检索框
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > span > span > input")
        #检查搜模板按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > span > span > span > button")
        #检查添加模板按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-heading > span > div > div > button")
        #检查公有库按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-footer > div > div.ant-tabs-nav > div.ant-tabs-nav-wrap > div > div.ant-tabs-tab.ant-tabs-tab-active")
        #检查私有库按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-footer > div > div.ant-tabs-nav > div.ant-tabs-nav-wrap > div > div:nth-child(2)")
        #检查分类标签
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-card > div > form > div:nth-child(1) > div.label___12r3x > span")
        #检查标签标签
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-card > div > form > div:nth-child(2) > div.label___12r3x > span")

        #添加屏幕截图
        QE_Driver.getScreenshot()

##########################################################################################################################

    #功能测试
    def TemplateFunctionTest(self):

        #测试添加模板功能

        #点击添加模板按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-heading > span > div > div > button").click()
        time.sleep(1)
        # 添加屏幕截图
        QE_Driver.getScreenshot()
        #点击保存按钮
        self.driver.find_element(By.CSS_SELECTOR, "#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button:nth-child(5)").click()
        time.sleep(1)
        # 添加屏幕截图
        QE_Driver.getScreenshot()
        #输入新增模板名称
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("测试添加模板功能")
        # 添加屏幕截图
        QE_Driver.getScreenshot()
        #分类输入二号
        self.driver.find_element(By.CSS_SELECTOR, "#category").send_keys("二号")
        # 添加屏幕截图
        QE_Driver.getScreenshot()
        #选择添加到公有库
        self.driver.find_element(By.CSS_SELECTOR, "#survey-modal-container > div > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-body > form > div:nth-child(4) > div > div > div > div > label > span.ant-checkbox > input").click()
        # 添加屏幕截图
        QE_Driver.getScreenshot()
        #点击保存按钮
        self.driver.find_element(By.CSS_SELECTOR, "#survey-modal-container > div > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-footer > button.ant-btn.ant-btn-primary").click()
        time.sleep(5)
        # 添加屏幕截图
        QE_Driver.getScreenshot()
        #点击关闭按钮
        self.driver.find_element(By.CSS_SELECTOR, "#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button.ant-btn.ant-btn-primary.ant-btn-sm.ant-btn-dangerous").click()
        time.sleep(1)
        # 添加屏幕截图
        QE_Driver.getScreenshot()

        #验证是否添加模板成功
        actual = self.driver.find_element(By.XPATH, "//*[contains(text(),'测试添加模板功能')]").text
        print(f"找到的文本内容: {actual}")
        #断言检测一下是否符合预期
        assert "测试添加模板功能" in actual

##########################################################################################################################

        #测试模板名字检索功能

        #模板检索框中输入模板名字“测试添加模板功能”
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > span > span > input").send_keys("测试添加模板功能")
        # 添加屏幕截图
        QE_Driver.getScreenshot()
        #点击搜模板按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-has-footer.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > span > span > span > button").click()
        time.sleep(1)
        # 添加屏幕截图
        QE_Driver.getScreenshot()
        #验证是否检索成功
        #若搜索出显示”暂无数据“则说明搜索功能异常，反之搜索功能成功
        actual = self.driver.find_element(By.XPATH, "//*[contains(text(),'暂无数据')]").text
        print(f"找到的文本内容: {actual}")
        #断言检测一下是否符合预期
        assert "暂无数据" in actual

        #Bug:搜模板搜索功能异常





# template = QE_TemplateTest()
# template.TemplateCheckElement()
# template.TemplateFunctionTest()

# 指定浏览器的退出
# QE_Driver.driver.quit()
